package com.starlight.flower_server.domain.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.exception.NotFoundException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.comment.cqrs.CommentPageQuery;
import com.starlight.flower_server.domain.comment.data.CommentData;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Comment;
import com.starlight.flower_server.infrastructure.entity.FlowerUser;
import com.starlight.flower_server.infrastructure.repository.ArticleRepository;
import com.starlight.flower_server.infrastructure.repository.CommentRepository;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRepository;
import com.starlight.flower_server.domain.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author yfy2001
 * @date 2023/4/6 8:48
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private CommentRepository commentRepository;

    @Resource
    private FlowerUserRepository flowerUserRepository;

    @Override
    public Comment queryCommentDetailById(Long id) {
        Comment comment= commentRepository.queryDetail(id);
        if (comment==null){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return comment;
    }

    @Override
    public Integer logicDeleteCommentById(Long id) {
        Comment comment=commentRepository.queryDetail(id);
        Integer i=commentRepository.logicDelete(id);
        if (!i.equals(0)){
            //删除成功，修改文章评论数-1
            articleRepository.addCommentNumByArticleId(comment.getArticleId(), -1);
        }else {
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return i;
    }

    @Override
    public Integer insertComment(Comment comment) {
        Integer i= commentRepository.insert(comment);
        if (!i.equals(0)){
            //修改文章评论数+1
            articleRepository.addCommentNumByArticleId(comment.getArticleId(), 1);
        }
        return i;
    }

    @Override
    public Integer modifyComment(Comment comment) {
        return commentRepository.modify(comment);
    }

    @Override
    public IPage<CommentData> queryCommentDataList(CommentPageQuery query) {
        IPage<Comment> page=commentRepository.queryPage(query);
        IPage<CommentData> data= page.convert(comment -> {
            CommentData commentData=new CommentData();
            BeanUtils.copyProperties(comment, commentData);
            return commentData;
        });
        List<CommentData> commentDataList=data.getRecords();
        setFlowerUserInfo(commentDataList);
        data.setRecords(commentDataList);
        return data;
    }

    @Override
    public List<CommentData> queryCommentDataListByArticleId(Long articleId) {
        List<Comment> commentList=commentRepository.queryListByArticleId(articleId);
        List<CommentData> commentDataList=commentList
                .stream().map(comment -> {
                    CommentData commentData=new CommentData();
                    BeanUtils.copyProperties(comment,commentData);
                    return commentData;
                }).toList();
        setFlowerUserInfo(commentDataList);
        return commentDataList;
    }

    @Override
    public TotalData<CommentData> queryAllCommentData() {
        TotalData<CommentData> data=new TotalData<>();
        List<Comment> commentList=commentRepository.queryAll();
        List<CommentData> commentDataList=commentList
                .stream().map(comment -> {
                    CommentData commentData=new CommentData();
                    BeanUtils.copyProperties(comment,commentData);
                    return commentData;
                }).toList();
        setFlowerUserInfo(commentDataList);
        Long total=(long)commentDataList.size();
        data.setTable("Comment");
        data.setStatTime(new Date());
        data.setTotal(total);
        data.setRecords(commentDataList);
        return data;
    }

    @Override
    public TotalData<CommentData> queryCommentTotalCount() {
        TotalData<CommentData> data=new TotalData<>();
        Long total=commentRepository.queryTotalCount();
        data.setTable("Comment");
        data.setStatTime(new Date());
        data.setTotal(total);
        return data;
    }

    private void setFlowerUserInfo(List<CommentData> commentDataList) {
        List<Long> flowerUserIdList=commentDataList.stream().map(CommentData::getFlowerUserId).toList();
        Map<Long, FlowerUser> flowerUserMap=flowerUserRepository.queryMap(flowerUserIdList);
        for (CommentData c:commentDataList) {
            FlowerUser flowerUser=flowerUserMap.get(c.getFlowerUserId());
            if (flowerUser!=null){
                c.setAuthor(flowerUser.getNickname());
            }else {
                c.setAuthor("此用户已经销");
            }
        }
    }
}
